﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MISAPP.Classify.Service;

public partial class View_CFG_CFG004 : ViewMatrix
{
    string uploadFile = string.Empty;
    string ym = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = "新增簽帳金額";

        if (!IsPostBack)
        {
            init();    
        }

    }

    protected void init()
    {
        Service_CFG004 cfg = new Service_CFG004();
        fillCategory(cfg.CardCategory, cfg);
        fillCategory(cfg.Rows, cfg);
        txtyyy.Text = (DateTime.Now.Year - 1911).ToString() ;
    }

    protected void fillCategory(string[] p_array,Service_CFG004 p_cfg)
    {
        foreach (string category in p_array)
        {
            WebControl cnt = null;
            string controlID = "txt{0}{1}";
            string[] cateArray = p_cfg.GetCardCellRange(category);
            for (int i = 0; i < cateArray.Length; i++)
            {
                cnt = findControlInContentPlaceHolder(
                    string.Format(controlID, category, (i + 1).ToString()));
                System.Reflection.PropertyInfo pi = cnt.GetType().GetProperty("Text");
                pi.SetValue(cnt, cateArray[i], null);
            }
        }
    }

    protected WebControl findControlInContentPlaceHolder(string p_conrolID)
    {
        if (mpContentPlaceHolder == null)
        {
            mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl(ContentPlaceHolderID);
        }
        return mpContentPlaceHolder.FindControl(p_conrolID) as WebControl;
    }

    protected void btnUPLoad_Click(object sender, EventArgs e)
    {
        if (!saveAS(upLoadCreditCard))
        { return; }

        Service_CFG004 cfg = new Service_CFG004();
        Squared_Excel seq_xls = new Squared_Excel();

        string[] card = {
              cfg.CardCategory[0] + "," + txtBANK卡1.Text + txtPROD_CATEGORY_L31.Text 
                + ":" + txtBANK卡2.Text + txtPROD_CATEGORY_L31.Text 

            , cfg.CardCategory[1] + ","  + txt認同卡1.Text + txtPROD_CATEGORY_L31.Text 
                + ":" + txt認同卡2.Text + txtPROD_CATEGORY_L31.Text 

            , cfg.CardCategory[2] + ","  + txt聯名卡1.Text + txtPROD_CATEGORY_L31.Text 
                + ":" + txt聯名卡2.Text + txtPROD_CATEGORY_L31.Text 
            , cfg.CardCategory[3] + ","  + txt公務採購卡1.Text 
                            };
        string[] money = {
             txtBANK卡1.Text + txt當月新增簽帳金額1.Text 
                + ":" + txtBANK卡2.Text + txt當月新增簽帳金額1.Text 
            , txt認同卡1.Text + txt當月新增簽帳金額1.Text 
                + ":" + txt認同卡2.Text + txt當月新增簽帳金額1.Text 
            , txt聯名卡1.Text + txt當月新增簽帳金額1.Text 
                + ":" + txt聯名卡2.Text + txt當月新增簽帳金額1.Text 
            , txt公務採購卡1.Text.Substring(0,2) + txt當月新增簽帳金額1.Text 
                         };

        seq_xls.UploadCardAmount(cfg, uploadFile, card, money, ym);
        seq_xls.ReleaseExcel();
    }

    protected string getCategory(string[] p_array, Service_CFG004 p_cfg)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        foreach (string category in p_array)
        {
            WebControl cnt = null;
            string controlID = "txt{0}{1}";
            string[] cateArray = p_cfg.GetCardCellRange(category);
            for (int i = 0; i < cateArray.Length; i++)
            {
                cnt = findControlInContentPlaceHolder(
                    string.Format(controlID, category, (i + 1).ToString()));
                System.Reflection.PropertyInfo pi = cnt.GetType().GetProperty("Text");
                sb.AppendFormat(",{0}", pi.GetValue(cnt,null).ToString());              
            }
        }
        return sb.ToString();
    }

    protected bool saveAS(System.Web.UI.WebControls.FileUpload upLoadCont)
    {
        bool ok = false; //沒有檔案就不往下做
        uploadFile = string.Empty;
        if (upLoadCont.HasFile)
        {
            uploadFile = Utility.SaveAS(upLoadCont, getResultValue(2)
            , Server.MapPath("~"));
            //卡別損益表_10010，找出是年月的字串
            ym = upLoadCreditCard.FileName.Substring(6).Replace(".xls", "");
            ok = true;
        }
        return ok;
    }

    protected void btnQuery_Click(object sender, EventArgs e)
    {
        Service_CFG004 cfg = new Service_CFG004();
        if (string.IsNullOrEmpty(txtyyy.Text))
        {
            return;
        }

        string dlFile = Utility.TemplateToDownload(
            Server.MapPath("~"), "CARD_AMOUNT.xls", "xls");
        Squared_Excel seq_xls = new Squared_Excel();

        seq_xls.GetCardAmount(cfg, dlFile,txtyyy.Text);

        linkExcel.NavigateUrl = string.Format("~/download/{0}",
                System.IO.Path.GetFileName(dlFile));
        linkExcel.Text = "目前結果";
        seq_xls.ReleaseExcel();
    }
}
